package com.testdocs.client.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PopupPanel;
import com.testdocs.client.style.CommonResources;

public class DropDownListWidget extends Composite {
	public static enum Order {
		TOP, BOTTOM;
	}

	private HorizontalPanel panel = new HorizontalPanel();
	private Image image = new Image();
	private Anchor textWidget = new Anchor("", false);
	private PopupPanel itemsListPanel = new PopupPanel(true);
	private MenuBar itemsListMenu = new MenuBar(true);

	private boolean needRebuild = true;
	private Map<String, MenuItem> items = new HashMap<String, MenuItem>();
	
	private List<String> itemsOrder = new ArrayList<String>();
	private String currentItemId;
	
	public DropDownListWidget() {
		CommonResources resources = (CommonResources) GWT.create(CommonResources.class);
		resources.style().ensureInjected();
		textWidget.addStyleName(resources.style().bold());
		
		itemsListPanel.add(itemsListMenu);

		ClickHandler handler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (items.size() > 1) {
					rebuildMenu();
					itemsListPanel.showRelativeTo(textWidget);
				}
			}
		};
		
		textWidget.addClickHandler(handler);
		image.addClickHandler(handler);
		
		image.setResource(resources.dropdown());
		
		panel.add(textWidget);
		panel.add(image);
		initWidget(panel);
	}

	public void addItem(final String id, String text, final Command command, Order order) {
		MenuItem menuItem = new MenuItem(text, new Command() {
			@Override
			public void execute() {
				onItemSelected(id);
				command.execute();
				itemsListPanel.hide();
			}
		});
		
		items.put(id, menuItem);
		if (order == Order.BOTTOM) {
			itemsOrder.add(id);
		} else {
			itemsOrder.add(0, id);
		}
		if (currentItemId == null) {
			onItemSelected(id);
		}
		needRebuild = true;
	}
	
	public void removeItem(String id) {
		items.remove(id);
		itemsOrder.remove(id);
		needRebuild = true;
	}
	
	public void setItemSelected(String id) {
		onItemSelected(id);
	}
	
	private void rebuildMenu() {
		if (needRebuild) {
			itemsListMenu.clearItems();
			for (String id : itemsOrder) {
				if (!id.equals(currentItemId)) {
					itemsListMenu.addItem(items.get(id));				
				}
			}
			needRebuild = false;
		}
	}
	
	private void onItemSelected(String id) {
		if (items.containsKey(id)) {
			textWidget.setText(items.get(id).getText());
			currentItemId = id;
			needRebuild = true;
		}
	}
}
